找传奇、传世资源到传世资源站!

测试网ip地址ping 结果

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

c# 测试网络是否稳定 通过ping ip又有异常就会记录下来

测试网ip地址ping 结果 C#网络编程-第1张测试网ip地址ping 结果 C#网络编程-第2张
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;//using System.Linq;using System.Text;using System.Windows;using System.Windows.Forms;using System.Net.NetworkInformation;using System.Text.RegularExpressions;namespace GeiIPState{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } string M_TbxStatus = ""; string GetIP = ""; string path = Application.StartupPath "\\IP_Status_Log.txt"; public void tbx_getIP_Leave() { try { int _int = GetIP.IndexOf("w"); if (_int >= 0 && GetIP.Substring(_int, 3) == "www") { if (IsUrl(GetIP.Trim())) { this.lab_prompt.Text = ""; return; } else { this.lab_prompt.Text = "格式有误!"; return; } } else { if (IPCheck(GetIP.Trim())) { this.lab_prompt.Text = ""; return; } else this.lab_prompt.Text = "格式有误!"; } } catch (Exception) { this.lab_prompt.Text = "格式有误!"; return; throw; } } public bool IPCheck(string IP) { try { string regformat = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"; Regex regex = new Regex(regformat, RegexOptions.IgnoreCase); return regex.IsMatch(IP); } catch (Exception) { this.lab_prompt.Text = "格式有误!"; throw; } } public bool IsUrl(string str_url) { try { return System.Text.RegularExpressions.Regex.IsMatch(str_url, @"(?<![\w@] )((http|https)://)?(www.)?[a-z0-9\.] (\.(com|net|cn|com\.cn|com\.net|net\.cn))(/[^\s\n]*)?"); } catch (Exception) { this.lab_prompt.Text = "格式有误!"; throw; } } public void Ping(string ip) { try { System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingReply reply = p.Send(ip); String Show_IP_States = ""; if (reply.Status.ToString() == "Success") { if (is_ip_status) { Show_IP_States = GetIP ": 时间【" reply.RoundtripTime "ms】 TTL【" reply.Options.Ttl "】"; Clear_Itme(Show_IP_States); } return; } else if (reply.Status.ToString() == "DestinationHostUnreachable") { M_TbxStatus = "[" DateTime.Now "] :" GetIP " 【无法访问目标主机】"; Show_IP_States= GetIP ": 【无法访问目标主机】"; Writer_Log(M_TbxStatus); if (is_ip_status) { Show_IP_States = GetIP ": 【无法访问目标主机】"; Clear_Itme(Show_IP_States); } } else { M_TbxStatus = "[" DateTime.Now "] :" GetIP " 【请求超时】"; Writer_Log(M_TbxStatus); if (is_ip_status) { Show_IP_States = GetIP ": 【请求超时】"; Clear_Itme(Show_IP_States); } } } catch (Exception) { this.lab_prompt.Text = "格式有误!"; return; throw; } } public void Clear_Itme(String Show_IP_States) { if (lbx_IPStatus.Items.Count == 50) lbx_IPStatus.Items.Clear(); lbx_IPStatus.Items.Add(Show_IP_States); } private void btn_start_Click(object sender, EventArgs e) { { GetIP = this.tbx_getIP.Text.ToLower(); if ( timer1.Enabled == false && GetIP.Trim()!="") { tbx_getIP_Leave(); if (this.lab_prompt.Text == "") { btn_start.Text = "停止"; timer1.Enabled = true; timer1.Start(); } } else if ( timer1.Enabled == true) { if (timer1.Enabled == true) { btn_start.Text = "开始"; timer1.Enabled = false; } } } } public void Writer_Log(String _log) { try { StreamWriter SW; if (!File.Exists(path)) { SW = File.CreateText(path); SW.Close(); } else { FileStream fs = new FileStream(path, FileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(_log); sw.Close(); fs.Close(); } } catch (Exception) { return; throw; } } private void timer1_Tick(object sender, EventArgs e) { try { if (timer1.Enabled == true) Ping(GetIP.Trim()); backgroundWorker1.RunWorkerAsync(); } catch (Exception) { return; throw; } } private void tbn_stop_Click(object sender, EventArgs e) { StreamWriter SW; if (File.Exists(path)) { System.Diagnostics.Process.Start(path); } else { SW = File.CreateText(path); SW.Close(); } } bool is_ip_status = true; private void ckbn_IPStatus_CheckedChanged(object sender, EventArgs e) { if (ckbn_IPStatus.Checked == true) is_ip_status = true; else is_ip_status = false; } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { timer1.Enabled = true; timer1.Start(); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复